<template>
  <div class="table-box">
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="书名">
        <el-input v-model="searchForm.bookName" placeholder="书名"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-input v-model="searchForm.bookType" placeholder="类型"></el-input>
      </el-form-item>
      <el-form-item label="出版日期">
        <el-date-picker
          v-model="searchForm.publishDate"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="bookName" label="书名" width="120" align="center">
        </el-table-column>
        <el-table-column label="封面" width="120" align="center">
          <template slot-scope="scope">
            <el-image
            :src="require('@/assets/normal/' + scope.row.image)"
            style="width: 80px; height: 60px;">
            </el-image>
          </template>
        </el-table-column>
        <!-- 多表联查，分类字段变更 -->
        <el-table-column prop="name" label="类型" align="center">
        </el-table-column>
        <el-table-column prop="bookNo" label="编号" width="120" align="center">
        </el-table-column>
        <el-table-column prop="author" label="作者" width="120" align="center">
        </el-table-column>
        <el-table-column prop="publisher" label="出版社" width="120" align="center">
        </el-table-column>
        <el-table-column prop="publishDate" label="出版日期" width="120" align="center">
        </el-table-column>
        <el-table-column prop="amount" label="数量" width="120">
        </el-table-column>
        <el-table-column prop="addTime" label="添加日期" width="200" align="center">
        </el-table-column>
        <el-table-column prop="updateUser" label="操作人" width="120" align="center">
        </el-table-column>
        <el-table-column prop="description" label="简介" width="200" align="center">
        </el-table-column>
        <el-table-column label="操作" width="160" fixed="right" align="center">
          <template slot-scope="scope">
            <!-- 使用嵌套表单的dialog -->
            <el-button type="primary" size="mini" @click="aditbook(scope.row.amount,scope.row.description,scope.row.bookName,scope.row.bookNo)">编辑</el-button>
            <el-button type="danger" size="mini" @click="deletebook(scope.row.bookNo,scope.row.image)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog title="编辑图书信息" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="书名" :label-width="formLabelWidth">
            <el-input
              v-model="this.form.bookName"
              :disabled="true">
            </el-input>
          </el-form-item>
          <el-form-item label="编号" :label-width="formLabelWidth">
            <el-input
              v-model="this.form.bookNo"
              :disabled="true">
            </el-input>
          </el-form-item>
          <el-form-item label="数量" :label-width="formLabelWidth">
            <el-input-number v-model="form.amount"></el-input-number>
          </el-form-item>
          <el-form-item label="简介" :label-width="formLabelWidth">
            <el-input type="textarea"
            :rows="3" 
            v-model="form.description"
            placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancleadit">取 消</el-button>
          <el-button type="primary" @click="adite">确 定</el-button>
        </div>
      </el-dialog>

      <br>
      <el-pagination background layout="total,sizes,prev,pager,next,jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :total="total"
      :current-page="currentPage"
      :page-size="pageSize"
      :page-sizes="[5,10,15,20,30,40,50]"></el-pagination>
  </div>
</template>

<script>
import axios from "axios"
export default {
    data() {
      return {
        tableData: null,
        searchForm: {
          bookName:'',
          bookType:'',
          publishDate:[],
        },
        pickDate:[],
        total:0,
        currentPage:1,
        pageSize:5,
        pickerOptions: {
          shortcuts: [{
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          },{
            text: '最近一年',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30 * 12);
              picker.$emit('pick', [start, end]);
            }
          },{
            text: '最近三年',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30 * 36);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        json:{},
        dialogFormVisible: false,
        form: {
          bookNo:null,
          bookName:null,
          amount:null,
          description:null
        },

      }
    },
    methods: {
      msToDate(msec){
            let datetime = new Date(msec);
            let year = datetime.getFullYear();
            let month = datetime.getMonth();
            let date = datetime.getDate();
           /*  let hour = datetime.getHours();
            let minute = datetime.getMinutes();
            let second = datetime.getSeconds(); */
        
            /* let result1 = year + 
                        '-' + 
                        ((month + 1) >= 10 ? (month + 1) : '0' + (month + 1)) + 
                        '-' + 
                        ((date + 1) < 10 ? '0' + date : date) + 
                        ' ' + 
                        ((hour + 1) < 10 ? '0' + hour : hour) +
                        ':' + 
                        ((minute + 1) < 10 ? '0' + minute : minute) + 
                        ':' + 
                        ((second + 1) < 10 ? '0' + second : second); */
        
            let result2 = year + 
                 '-' + 
                 ((month + 1) >= 10 ? (month + 1) : '0' + (month + 1)) + 
                 '-' + 
                 ((date + 1) < 10 ? '0' + date : date);
        
            return result2;
        },
      search() {
          this.json = {}
          this.$set(this.json,"page",this.currentPage)
          this.$set(this.json,"pageSize",this.pageSize)
          if(this.searchForm.bookName != ''){
            this.$set(this.json,"bookName",this.searchForm.bookName.trim())
          }
          if(this.searchForm.bookType != ''){
            this.$set(this.json,"bookType",this.searchForm.bookType.trim())
          }
          if(this.searchForm.publishDate != null && this.searchForm.publishDate.length == 2){
            this.$set(this.json,"startTime",this.msToDate(this.searchForm.publishDate[0]))
            this.$set(this.json,"endTime",this.msToDate(this.searchForm.publishDate[1]))
          }
          axios.post(this.url + "/book/list",this.json)
        .then((res) => {
          if(res.data.code == 1){
            this.total = parseInt(res.data.data.total)
            this.tableData = res.data.data.records
          }
        }).catch(() => {
          this.$message.error("加载失败")
        });
                                 
      },
      handleSizeChange:function(val){
        console.log(this.json)
        this.$set(this.json,"pageSize",val) 
        axios.post(this.url + "/book/list",this.json)
        .then((res) => {
          if(res.data.code == 1){
            this.total = parseInt(res.data.data.total)
            this.tableData = res.data.data.records
          }
        }).catch(() => {
          this.$message.error("加载失败")
        });
      },
      handleCurrentChange:function(val){
        this.$set(this.json,"page",val)
        axios.post(this.url + "/book/list",this.json)
        .then((res) => {
          if(res.data.code == 1){
            this.total = parseInt(res.data.data.total)
            this.tableData = res.data.data.records
          }
        }).catch(() => {
          this.$message.error("加载失败")
        });
      },
      getbook(){
        axios.post(this.url + "/book/list",this.json)
        .then((res) => {
          if(res.data.code == 1){
            this.total = parseInt(res.data.data.total)
            this.tableData = res.data.data.records
          }
        }).catch(() => {
          this.$message.error("加载失败")
        });
      },
      
      adite(){
        if(this.form.description == null || this.form.description == ''){
          this.$message.warning("描述不能为空")
          return
        }
        axios.put(this.url+"/book/updatebook",{
          "bookNo":this.form.bookNo,
          "amount":this.form.amount,
          "description":this.form.description,
          "updateUser":localStorage.getItem("username")
        }).then((res) => {
          if(res.data.code == 1){
            this.$message.success("操作成功")
            this.cancleadit()
            this.getbook()
          }
        }).catch(() => {
          this.$message.error("修改失败")
          return
        });
      },

      cancleadit(){
        this.dialogFormVisible = false
        this.form.amount=null
        this.form.description=null
        this.form.bookName=null
        this.form.bookNo=null
      },


      aditbook(val1,val2,val3,val4){
        this.form.amount=val1,
        this.form.description=val2
        this.form.bookName=val3
        this.form.bookNo=val4
        this.dialogFormVisible = true
      },

      /* 控制函数的执行顺序 */
       deletebook(val1,val2){
        /* 删除图书 */
       axios.post(this.url+"/book/deletebook",{
          "bookNo":val1
        }).then((res) => {
          if(res.data.code == 1){
            this.$message.success("删除图书成功")
                /* 删除图片 */
            axios.get(this.url + "/upload/dimage",{
                        params:{
                            fileName:val2
                        }     
                    }).then(() => {
                        
                    }).catch(() => {
                        this.$message.error("出现错误,请联系管理员")
                    })
            this.getbook()
          }else{
            this.$message.error(res.data.msg)
            return
          }
        }).catch(() => {
          this.$message.error("操作失败")
          return
        });

        
      }
    },
    mounted () {
      this.$set(this.json,"page",this.currentPage)
      this.$set(this.json,"pageSize",this.pageSize) 
      axios.post(this.url + "/book/list",this.json)
      .then((res) => {
        if(res.data.code == 1){
          this.total = parseInt(res.data.data.total)
          this.tableData = res.data.data.records
        }
      }).catch(() => {
        this.$message.error("加载失败")
      });
    } 
}
</script>

<style>
.table-box{
  position: relative;
  width: 100%;
  height: 100%;
}
</style>